package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.my.adapter.MyDataAdapter;
import com.jp.knowledge.my.adapter.MyMoreAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.business.MyMainBusiness;
import com.jp.knowledge.my.business.Vector;
import com.jp.knowledge.my.model.ActiveDataModel;
import com.jp.knowledge.my.model.OptionSelectModel;
import com.jp.knowledge.my.view.RoundRectView;
import com.jp.knowledge.my.view.ShareInvitePw;
import com.jp.knowledge.view.RecycleViewDiver;

import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的
 *
 * @author gzejia 978862664@qq.com
 */
public class MyMainActivity extends SlidingActivity {

    @ViewInject(R.id.my_iv_header)
    private ImageView mHeaderIv;

    @ViewInject(R.id.my_tv_user)
    private TextView mUserTv;

    @ViewInject(R.id.my_round_v_message)
    private RoundRectView mMessageRoundV;

    @ViewInject(R.id.my_iv_grade)
    private ImageView mGradeIv;

    @ViewInject(R.id.my_tv_grad)
    private TextView mGradeTv;

    @ViewInject(R.id.my_rv_more)
    private RecyclerView mMoreRv;

    @ViewInject(R.id.my_rv_data)
    private RecyclerView mDataRv;

    @ViewInject(R.id.my_tv_role)
    private TextView mRoleTv;

    @ViewInject(R.id.my_tv_organize)
    private TextView mOrganizeTv;

    @ViewInject(R.id.my_tv_franchise)
    private TextView mFranchiseTv;

    @ViewInject(R.id.my_rv_attestation)
    private RecyclerView mAttestationRv;

    @ViewInject(R.id.my_tv_share)
    private TextView mShareTv;

    @ViewInject(R.id.my_round_v_share)
    private RoundRectView mShareRoundV;

    private MyMainBusiness mBusiness = new MyMainBusiness();
    boolean isLogin = true;// 模拟登录状态

    @Override
    protected int getResourceId() {
        return R.layout.activity_my_main;
    }

    @Override
    protected void init() {
        initTitle();
        initOthers(isLogin);
        initDataRv(mBusiness.getActiveData(getActiveIndex(isLogin),application.getUserData()));
        initMoreRv(mBusiness.getMoreItems());
//        initAttestationRv(mBusiness.getAttestationItems(MyMainBusiness.GO_AUDIT));
    }

    @Event(value = {R.id.my_iv_message, R.id.my_tv_message, R.id.my_iv_header, R.id.my_iv_grade,
            R.id.my_tv_grad, R.id.my_tv_achievement, R.id.my_tv_franchise, R.id.my_lly_role,
            R.id.my_lly_organize, R.id.my_lly_help, R.id.my_lly_share, R.id.my_lly_set},
            type = View.OnClickListener.class)
    private void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_iv_message:// 消息
            case R.id.my_tv_message:
                // 登录：查看用户信息；未登录：跳转至登录页

                break;
            case R.id.my_iv_header:// 头像
            case R.id.my_tv_user:// 用户名
                // 登录：查看用户信息；未登录：跳转至登录页
                Intent personalIntent = new Intent(this, QrCodeActivity.class);
                personalIntent.putExtra(Vector.TRANSFER_INTEGER + "qr_scan_type",
                        QrCodeActivity.QR_CODE_PERSONAL);
                startActivity(personalIntent);
                break;
            case R.id.my_iv_grade:// 级别
            case R.id.my_tv_grad:
                startActivity(new Intent(this, MyVipActivity.class));
                break;
            case R.id.my_tv_achievement:// 成就
                break;
            case R.id.my_tv_franchise:// 特权
                break;
            case R.id.my_lly_role:// 角色
                startActivity(new Intent(this, MyRoleActivity.class));
                break;
            case R.id.my_lly_organize:// 组织
                Intent organizeIntent = new Intent();
//                intent.setBranch(this, OrganizeJoinActivity.class);// 加入组织
                organizeIntent.setClass(this, IdentifyActivity.class);// 公司/众创空间/班级认证
                organizeIntent.putExtra(Vector.TRANSFER_INTEGER + "identify_type", IdentifyActivity.IDENTIFY_PCREATION);
                startActivity(organizeIntent);
                break;
            case R.id.my_lly_help:// 帮助
                break;
            case R.id.my_lly_share:// 分享
                ShareInvitePw shareInvitePw = new ShareInvitePw(
                        MyMainActivity.this, ShareInvitePw.POPWIN_TYPE_SHARE);
                shareInvitePw.addOnOptionSelectListener(mOnShareSelect);
                shareInvitePw.showAsDropDown(leftIcon);
                break;
            case R.id.my_lly_set:// 设置
                startActivity(new Intent(this, SetActivity.class));
                break;
        }
    }

    private void initTitle() {
        topName.setText(R.string.my_tv_main);
        topDesc.setVisibility(View.GONE);
        leftIcon.setVisibility(View.GONE);
        rightIcon.setVisibility(View.GONE);
    }

    private void initDataRv(List<ActiveDataModel> list) {
        mDataRv.setHasFixedSize(true);
        mDataRv.setLayoutManager(new LinearLayoutManager(
                this, LinearLayoutManager.HORIZONTAL, false));
        mDataRv.addItemDecoration(new RecycleViewDiver(this, LinearLayout.VERTICAL));
        MyDataAdapter mDataAdapter = new MyDataAdapter(this, list);
        mDataAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(MyMainActivity.this, MyVieCoinActivity.class));
                        break;
                }
            }
        });
        mDataRv.setAdapter(mDataAdapter);
    }

    private void initAttestationRv(List<OptionSelectModel> list) {
        mAttestationRv.setHasFixedSize(true);
        mAttestationRv.setLayoutManager(new LinearLayoutManager(
                this, LinearLayoutManager.HORIZONTAL, false));
        MyMoreAdapter mMoreAdapter = new MyMoreAdapter(this, list);
        mMoreAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                switch (position) {
                    case 0:// 加入审核
                        startActivity(new Intent(MyMainActivity.this, AuditJoinActivity.class));
                        break;
                    case 1:// 公司通讯录
                        Intent companyIntent = new Intent(MyMainActivity.this, ContactsActivity.class);
                        companyIntent.putExtra(Vector.TRANSFER_INTEGER + "contacts_type",
                                ContactsActivity.CONTACTS_COMPANY);
                        startActivity(companyIntent);
                        break;
                    case 2:// 邀请同事
                        ShareInvitePw shareInvitePw = new ShareInvitePw(
                                MyMainActivity.this, ShareInvitePw.POPWIN_TYPE_INVITE);
                        shareInvitePw.addOnOptionSelectListener(mOnInviteSelect);
                        shareInvitePw.showAsDropDown(leftIcon);
                        break;
                    case 3:// 公司特权
                        startActivity(new Intent(MyMainActivity.this, OrganizeFranchiseActivity.class));
                        break;
                }
            }
        });
        mAttestationRv.setAdapter(mMoreAdapter);
    }

    private void initMoreRv(List<OptionSelectModel> list) {
        mMoreRv.setHasFixedSize(true);
        mMoreRv.setLayoutManager(new LinearLayoutManager(
                this, LinearLayoutManager.HORIZONTAL, false));
        MyMoreAdapter mMoreAdapter = new MyMoreAdapter(this, list);
        mMoreAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
            @Override
            public void itemSelect(int position) {
                switch (position) {
                    case 3:// 红包
                        startActivity(new Intent(MyMainActivity.this, MyReeceActivity.class));
                        break;
                    case 4:// 档案
                        startActivity(new Intent(MyMainActivity.this, MyFileActivity.class));
                        break;
                }
            }
        });
        mMoreRv.setAdapter(mMoreAdapter);
    }

    private void initOthers(boolean isLogin) {
        if (isLogin) {
            mUserTv.setText("巫海青");
            mUserTv.setTextColor(Color.WHITE);
            mMessageRoundV.setText("8");
            mMessageRoundV.setVisibility(View.VISIBLE);
            mFranchiseTv.setVisibility(View.VISIBLE);
            mHeaderIv.setImageResource(R.mipmap.ic_my_header);
            x.image().bind(mHeaderIv, "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3461331753,1116232261&fm=116&gp=0.jpg",
                    Vector.IMAGE_OPTIONS);

            mGradeIv.setImageResource(R.mipmap.ic_my_message);
            mGradeTv.setText("初级管家");
            mRoleTv.setText("竞品说管家");
            mOrganizeTv.setText("广州创将信息科技有限公司（请认证公司）");
        } else {
            mUserTv.setText("登录");
            mUserTv.setTextColor(Color.YELLOW);
            mMessageRoundV.setVisibility(View.GONE);
            mFranchiseTv.setVisibility(View.GONE);
        }
        mShareRoundV.setVisibility(View.VISIBLE);
        mShareTv.setText("奖励分享");
    }

    /**
     * @return 假定用户活跃数据数据量
     */
    private List<Integer> getActiveIndex(boolean isLogin) {
        List<Integer> index = new ArrayList<>();
        for (int i = 0, k = MyMainBusiness.ACTIVE_SIZE; i < k; i++) {
            index.add(isLogin ? (int) (Math.random() * 399) : 0);
        }
        return index;
    }

    /**
     * 邀请对话框选项监听
     */
    ShareInvitePw.OnOptionSelectListener mOnInviteSelect = new ShareInvitePw.OnOptionSelectListener() {
        @Override
        public void quoteClick(int position) {
            switch (position) {
                case 0:// 手机通讯录
                    Intent companyIntent = new Intent(MyMainActivity.this, ContactsActivity.class);
                    companyIntent.putExtra(Vector.TRANSFER_INTEGER + "contacts_type",
                            ContactsActivity.CONTACTS_PHONE);
                    startActivity(companyIntent);
                    break;
                case 3:// 二维码邀请
                    Intent intent = new Intent(MyMainActivity.this, QrCodeActivity.class);
                    intent.putExtra(Vector.TRANSFER_INTEGER + "qr_scan_type",
                            QrCodeActivity.QR_CODE_ORGANIZE);
                    startActivity(intent);
                    break;
                default:
                    ToasUtil.toast(MyMainActivity.this, "选中：" + position);
                    break;
            }
        }

        @Override
        public void systemClick(int position) {
            switch (position) {
                case 0:// 电脑端导入
                    startActivity(new Intent(MyMainActivity.this, PcImportActivity.class));
                    break;
                default:
                    ToasUtil.toast(MyMainActivity.this, "选中：" + position);
                    break;
            }
        }
    };

    /**
     * 分享对话框选项监听
     */
    ShareInvitePw.OnOptionSelectListener mOnShareSelect = new ShareInvitePw.OnOptionSelectListener() {
        @Override
        public void quoteClick(int position) {
            switch (position) {
                default:
                    ToasUtil.toast(MyMainActivity.this, "选中：" + position);
                    break;
            }
        }

        @Override
        public void systemClick(int position) {
            ToasUtil.toast(MyMainActivity.this, "选中：" + position);
        }
    };
}
